/***************************************************************************
 *            mainwindow.h
 *
 *  Fri Jan  5 14:05:43 2007
 *  Copyright  2007  Min Seok Seo
 *  <msseo@oasyss.co.kr>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with main.c; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#ifndef LINUXPOS_MAIN_WINDOW_H
#define LINUXPOS_MAIN_WINDOW_H

#include <iostream>
#include <iomanip>
#include <libglademm/variablesmap.h>
#include <libglademm/xml.h>
#include <glibmm.h>
#include <glib.h>
#include <gtkmm.h>
#include <gtkmm/button.h>
#include <gtkmm/window.h>
#include <pqxx/connection>
#include <pqxx/transaction>
#include <pqxx/result>
#include <control/CashDrawerControl.h>
#include <control/POSPrinterControl.h>
#include <control/LineDisplayControl.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <time.h>
#include "usbcid.h"
#include "sales.h"
#include "cellitem.h"

using namespace PGSTD;
using namespace pqxx;
using namespace std;
using namespace booyopos;

class MainWindow : public Gtk::Window 
{
public:
	MainWindow(std::string get_sosok, std::string get_userid);	
	virtual ~MainWindow();
	Sales sales;

protected:
	CCashDrawer CashDrawer;
	CPOSPrinter POSPrinter;

	// 윈도우 초기화 함수
	void Init();
	// 중단 테이블 변경함수 (테이블, 메뉴, 계산)
	void table_show(int page);
	// 테이블 변경함수
	void table_change(Glib::ustring data);
	// 캐쉬 정보 이동
	void CashTransfer(string nowACCT, string SEQ, string transACCT, string transBILL);
	// 메뉴화면
	void menu_show(std::string menu_grup_code);
	// 계산화면
	void pay_show();

	// 하단 테이블 변경함수 
	void table_low_show();
	void menu_low_show();
	void pay_low_show();
	
	// 버튼 초기화 함수
	void initbutton();
	void initlowbutton();
	
	// 세부 금액 표시란 변경 함수
	void price_info();
	// 트리뷰 초기화 함수
	void treeview_init();
	// 체크인 저장함수
	void cheksave();
	// 체크아웃 함수
	void chekout();
	// 구입 항목 저장 함수
	void cashsave();
	// acct 구하기
	void acctnum();
	// bill 구하기
	void billnum();
	// 영수증 정보 
	void billinsert();
		
	// 메뉴 트리 추가 핸들러
	virtual void liststore_add_item(const CellItem_Product& foo);
	// 테이블 버튼 이벤트 핸들러
	virtual void on_Button_Table_clicked(std::string data);
	virtual void on_Button_Menuadd_clicked(std::string data);
	virtual void on_Button_Chekin_Table_clicked(std::string data);
	// 테이블 하단 버튼 이벤트 핸들러
	virtual void on_Button_Table_Low_clicked(Glib::ustring data);
	// 메뉴 버튼 이벤트 핸들러
	virtual void on_Button_Menu_clicked(Glib::ustring data);
	// 메뉴 하단 버튼 이벤트 핸들러
	virtual void on_Button_Menu_Low_clicked(Glib::ustring data);
	// 계산 버튼 이벤트 핸들러
	virtual void on_Button_Pay_clicked(Glib::ustring data);
	// 계산 하단 버튼 이벤트 핸들러
	virtual void on_Button_Pay_Low_clicked(Glib::ustring data);
	// 메뉴 추가 제거 이벤트 핸들러
	virtual void on_Button_Menu_add_clicked();
  	virtual void on_Button_Menu_remove_clicked();
  	virtual void on_Button_Menu_up_clicked();
  	virtual void on_Button_Menu_down_clicked();
  	// 자세히 버튼 핸들러
  	virtual void on_Button_Detail_clicked();
	// 종료 이벤트 핸들러
	virtual void on_Button_Exit_clicked();
	
	// 타이머 함수
	bool onSecondElapsed(void);
	
	// DB 접속 클래스
	connection C;
	// 글래이드 파일 받아오는 변수
	Glib::RefPtr<Gnome::Glade::Xml> m_refGlade;
	Gnome::Glade::VariablesMap* m_pMainWindowMap;

	// 상단 라벨
	Gtk::Label* m_Label_Ujname;		 /* 업장이름 */
	Gtk::Label* m_Label_Workdate;	 /* 영업일자 */
	Gtk::Label* m_Label_Nowdate;	 /* 현재일자 */
	Gtk::Label* m_Label_User;		 /* 사용자 */
	Gtk::Label* m_Label_Time;		 /* 현재시간 */
	Gtk::Label* m_Label_Ver;		 /* 버젼 */
	Gtk::Label* m_Label_Total_Person;/* 방문객 */
	Gtk::Label* m_Label_Total_Price; /* 총수입  */
	
	// 금액 라벨	
	Gtk::Label* m_Label_Price_info[8];
	/* [0] 총금액 */
	/* [1] 할인율 */
	/* [2] 판매액 */
	/* [3] 봉사료 */
	/* [4] 부가세 */
	/* [5] 청구액 */
	/* [6] 입   금 */
	/* [7] 잔   액 */
	
	// 중단 테이블 (5x8)
	Gtk::Table* m_Table;
	// 하단 테이블 (5x2)
	Gtk::Table* m_Table_Low[2];
	// 금액 정보 테이블 (1x5)
	Gtk::Table* m_Table_Price_L;
	// 금액 정보 테이블 (1x3)
	Gtk::Table* m_Table_Price_R;
	
	// 테이블 버튼
	Gtk::Button* m_Button_Table[5][8];
	// 메뉴 버튼
	Gtk::Button* m_Button_Menu[5][8];	
	// 계산 버튼
	Gtk::Button* m_Button_Pay[5][8];
	// 테이블 하단 버튼
	Gtk::Button* m_Button_Table_Low[5][2];
	// 메뉴 하단 버튼
	Gtk::Button* m_Button_Menu_Low[5][2];
	// 메뉴 하단 버튼
	Gtk::Button* m_Button_Pay_Low[5][2];
	
	// 세부 메뉴 항목 트리뷰
	Gtk::TreeView* m_TreeView_Menu;
	Gtk::TreeView* m_TreeView_Detail;
	
	// 트리뷰 리스트
	Glib::RefPtr<Gtk::ListStore> m_refListStore;
	Glib::RefPtr<Gtk::ListStore> m_refListStore_Detail;
	typedef Gtk::TreeModel::Children type_children;
	struct ModelColumns : public Gtk::TreeModelColumnRecord
	{	
		Gtk::TreeModelColumn<Glib::ustring> product;/*상품이름*/
    	Gtk::TreeModelColumn<int>           number;	/*수량*/
    	Gtk::TreeModelColumn<Glib::ustring> price;	/*합계표시*/
    	Gtk::TreeModelColumn<int>           code;	/*메뉴코드*/
    	Gtk::TreeModelColumn<int>           div;	/*세금*/
    	Gtk::TreeModelColumn<int>           svc;	/*봉사료*/
    	Gtk::TreeModelColumn<int>           vat;	/*부가세*/
    	Gtk::TreeModelColumn<int>           adv;	/*0*/
    	Gtk::TreeModelColumn<int>           rate;	/*단가*/
    	Gtk::TreeModelColumn<int>           net;	/*회사수입*/
    	Gtk::TreeModelColumn<int>           tot;	/*합계*/
    	Gtk::TreeModelColumn<int>           dc;		/*DC*/
		Gtk::TreeModelColumn<int>           credit;	/*입금*/
    	ModelColumns() { add(number); add(product); add(price); add(code); add(div); add(svc); \
						 add(vat); add(adv); add(rate); add(net); add(tot); add(dc); add(credit);}
    };
	const ModelColumns m_columns;
	struct DetailColumns : public Gtk::TreeModelColumnRecord
	{	
		Gtk::TreeModelColumn<Glib::ustring> text;
	   	Gtk::TreeModelColumn<Glib::ustring> contents;
    	DetailColumns() { add(text); add(contents);}
    };
	const DetailColumns m_detail_columns;

  	//Variables
  	Glib::ustring windowstatus;    // 현재 윈도우 상태 (table,menu,pay 구분)
	Glib::ustring tablestatus;	   // 현재 테이블 상태 (보통normal,이체transfer,변경change,합산tableadd)
	Glib::ustring menustatus;	   // 메뉴 상태 (신규, 추가)
	Glib::ustring printstatus;	   // Print 항목 선택 
  	std::string check_menu_grp;
	std::string SQL;

	
	//**************************************************************************
	// 하부 윈도우 관련
	//**************************************************************************
	// 테이블 이체 
	Gtk::Window* tabletranswindow;
	Gtk::TreeView* tabletranstreeview;
	void tabletranswindow_init();
	virtual void on_Trans_Button_Cancel_clicked();
	virtual void on_Trans_Button_Enter_clicked();
	virtual void on_Trans_Button_Allselect_clicked();
	virtual void on_Trans_Button_Allunselect_clicked();
	void selected_row_callback(const Gtk::TreeModel::iterator& iter);
	bool select_function(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::Path& path, bool);
	// 자세히
	Gtk::Window* menudetailwindow;
	Gtk::TreeView* m_MenuDetailTreeview;
	void menudetailwindow_init();
	virtual void on_MenuDetail_Button_Cancel_clicked();
	// 보고서
	Gtk::Window* reportwindow;
	Glib::ustring m_Report_Entry_Ujcd;
	Glib::ustring m_Report_Entry_Todate;
	Glib::ustring m_Report_Entry_Fromdate;
	Gtk::Calendar* m_FromdateCalendar;
	Gtk::Calendar* m_TodateCalendar;
	Gtk::Label* m_Report_Label_Menu;
	Gtk::Label* m_Report_Label_Charge;
	Gtk::Label* m_Report_Label_Credit;
	Gtk::TreeView* m_ReportMenuTreeview;
	Gtk::TreeView* m_ReportChargeTreeview;
	Gtk::TreeView* m_ReportCreditTreeview;
	void reportwindow_init();
	void reportwindow_show();
	virtual void on_Report_Button_Cancel_clicked();
	virtual void on_Report_Button_Enter_clicked();
	virtual void on_Report_Button_Print_clicked();
	virtual void From_day_selected();
	virtual void To_day_selected();
	Glib::Date get_from_date() const;
	Glib::Date get_to_date() const;
	/* 품목현황 */
	Glib::RefPtr<Gtk::ListStore> m_refListReportMenu;
	struct ReportMenuModelColumns : public Gtk::TreeModelColumnRecord
	{	
		Gtk::TreeModelColumn<Glib::ustring> deta;	  /*메뉴명*/    	
		Gtk::TreeModelColumn<Glib::ustring> rate;     /*단가*/
    	Gtk::TreeModelColumn<Glib::ustring> tot;      /*판매금액*/
    	Gtk::TreeModelColumn<Glib::ustring> cnt;      /*판매수량*/
		Gtk::TreeModelColumn<Glib::ustring> net;      /*매점수입*/    	
		Gtk::TreeModelColumn<Glib::ustring> svc;      /*봉사료*/
    	Gtk::TreeModelColumn<Glib::ustring> vat;      /*부가세*/
    	Gtk::TreeModelColumn<Glib::ustring> gtot;     /*합계*/
    	ReportMenuModelColumns() { add(deta); add(rate); add(tot); add(cnt); add(net); add(svc); add(vat); add(gtot);}
    }; const ReportMenuModelColumns m_report_menu_columns;
	/* 판매현황 */
	Glib::RefPtr<Gtk::ListStore> m_refListReportCharge;
	struct ReportChargeModelColumns : public Gtk::TreeModelColumnRecord
	{	
		Gtk::TreeModelColumn<Glib::ustring> workdate; /*영업일자*/
		Gtk::TreeModelColumn<Glib::ustring> billnum;  /*빌번호*/
		Gtk::TreeModelColumn<Glib::ustring> tablnum;  /*테이블번호*/
		Gtk::TreeModelColumn<Glib::ustring> rate;     /*메뉴금액*/
    	Gtk::TreeModelColumn<Glib::ustring> dc;       /*DC*/
		Gtk::TreeModelColumn<Glib::ustring> net;      /*매점수입*/
    	Gtk::TreeModelColumn<Glib::ustring> svc;      /*봉사료*/
    	Gtk::TreeModelColumn<Glib::ustring> vat;      /*부가세*/
    	Gtk::TreeModelColumn<Glib::ustring> gtot;     /*합계*/
    	ReportChargeModelColumns() { add(workdate); add(billnum); add(tablnum); add(rate); \
									 add(dc); add(net); add(svc); add(vat); add(gtot);}
    }; const ReportChargeModelColumns m_report_charge_columns;
	/* 입금현황 */
	Glib::RefPtr<Gtk::ListStore> m_refListReportCredit;
	struct ReportCreditModelColumns : public Gtk::TreeModelColumnRecord
	{	
		Gtk::TreeModelColumn<Glib::ustring> workdate; /*영업일자*/
		Gtk::TreeModelColumn<Glib::ustring> billnum;  /*빌번호*/
		Gtk::TreeModelColumn<Glib::ustring> tablnum;  /*테이블번호*/
		Gtk::TreeModelColumn<Glib::ustring> cash;  	  /*현금*/
		Gtk::TreeModelColumn<Glib::ustring> card;  	  /*카드*/
		Gtk::TreeModelColumn<Glib::ustring> later;    /*후불*/
		Gtk::TreeModelColumn<Glib::ustring> jubdae;   /*접대*/
    	ReportCreditModelColumns() { add(workdate); add(billnum); add(tablnum); add(cash); add(card); \
						 add(later); add(jubdae); }
    }; const ReportCreditModelColumns m_report_credit_columns;
	// 마감
	Gtk::Window* workclosewindow;
	void workclosewindow_init();
	Glib::ustring m_WorkClose_Entry_User;
	Glib::ustring m_WorkClose_Entry_Ujname;
	Glib::ustring m_WorkClose_Entry_Workdate;
	virtual void on_WorkClose_Button_Enter_clicked();
	virtual void on_WorkClose_Button_Cancel_clicked();
	// 메뉴 찾기
	Gtk::Window* menufindwindow;
	Gtk::TreeView* menufindtreeview;
	void menufindwindow_init();
	virtual void on_MenuFind_Button_Cancel_clicked();
	// Cash_Window
	ostringstream getnumber;
	Gtk::Window* cashwindow;
	void cashwindow_init();
	void cashwindow_show();
	virtual void on_Cash_Button_Cancel_clicked();
	virtual void on_Cash_Button_Enter_clicked();
	virtual void on_Cash_Button_Numpad_clicked(Glib::ustring data);
	virtual void on_Cash_Button_Clear_clicked();
	Glib::ustring m_Cash_Entry_remain;
	Glib::ustring m_Cash_Entry_get;
	Glib::ustring m_Cash_Entry_give;
	int Cash_Remain;
	int Cash_Get;
	int Cash_Give;
	// Card_Window
	Gtk::Window* cardwindow;
	void cardwindow_init();
	void cardwindow_show(Glib::ustring cardname);
	virtual void on_Card_Button_Cancel_clicked();
	virtual void on_Card_Button_Enter_clicked();
	virtual void on_Card_Button_Numpad_clicked(Glib::ustring data);
	virtual void on_Card_Button_Clear_clicked();
	Glib::ustring m_Card_Entry_cardname;
	Gtk::Entry* m_Card_Entry_number;
	Glib::ustring m_Card_Entry_price;
	int Card_Remain;
	int Card_Get;
	// Cntwindow
	Gtk::Window* cntwindow;
	void cntwindow_init();
	void cntwindow_show();
	virtual void on_Cnt_Button_Cancel_clicked();
	virtual void on_Cnt_Button_Enter_clicked();
	virtual void on_Cnt_Button_Numpad_clicked(Glib::ustring data);
	virtual void on_Cnt_Button_Clear_clicked();
	Glib::ustring m_Cnt_Entry;
	// 빌찾기
	Gtk::Window* billfindwindow;
	void billfindwindow_init();
	virtual void on_BillFind_Button_Cancel_clicked();
	virtual void on_BillFind_Button_Enter_clicked();
	virtual void on_BillFind_Button_Numpad_clicked(Glib::ustring data);
	virtual void on_BillFind_Button_Clear_clicked();
	Glib::ustring m_BillFind_Entry;
	// 빌정보
	Gtk::Window* billwindow;
	Gtk::TreeView* billmenutreeview;
	Gtk::TreeView* billdetailtreeview;
	void billwindow_init();
	void billwindow_show(Glib::ustring bill);
	virtual void on_Bill_Button_Void_clicked();
	virtual void on_Bill_Button_Rebill_clicked();
	virtual void on_Bill_Button_Close_clicked();
	virtual void on_Bill_Button_Detail_clicked();
	Glib::ustring m_Bill_Label[8];
	// Void Memo
	Gtk::Window* voidmemo;
	void voidmemo_init();
	virtual void on_Voidmemo_Button_Cancel_clicked();
	virtual void on_Voidmemo_Button_Enter_clicked();
	Glib::ustring m_Voidmemo_Entry;
	// MSG window
	Gtk::Window* msgwindow;
	Gtk::Label* m_MSG_Label;
	void msgwindow_init();
	void msgwindow_show(Glib::ustring msg);
	virtual void on_MSG_Button_clicked();
	// comma
	std::string sep_thousands(int i);
	// PRINT
	void PRINT();
	void CASHDRAWER();
	void LINEDISPLAY();
};

#endif // LINUXPOS_MAIN_WINDOW_H
